﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using PMC;
using PMC.Extensions;
using PMC.Plugins;
using PMC.Scripts;
using PMC.Structs;
using PMC.Urls;
using System.Linq;

namespace PMCgui
{
    public partial class Options : Form
    {
        private List<UriCollector> collectors;
        private List<Plugin<IExtension>> extensions = Plugin<IExtension>.Plugins.ToList();

        public Options()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            tabWebsites.Controls.Add(new WebsitesTab());
            collectors = Plugin<UriCollector>.Plugins.Instances().ToList();
            cbProtectedMode.Checked = !Global.GetIEProtectedMode();

            Dock = DockStyle.Fill;
            cbUrlCollectors.DataSource = collectors;
            cbUrlCollectors.SelectedItem = UriCollector.ActiveCollector;

            tabExpression.Controls.Add(new ExpressionsTab());
            //PMC.Properties.Settings.Default.Save(); //Standard settings may be applied //todo what does this line do?

            foreach (var extension in extensions)
            {
                tabMain.Controls.Add(extension.Instance.SettingsControl);
            }
            tabMain.TabPages.Remove(tabGlobal); //TODO tijdelijk, even kijken of ik deze moet houden
        }

        /// <summary>
        /// Method bound to btSettings.Click event; Shows additional settings for the selected UrlCollector
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btSettings_Click(object sender, EventArgs e)
        {
            var result = ((UriCollector)cbUrlCollectors.SelectedItem).ShowSettings();
            if (result == DialogResult.OK)
            {
                UriCollector.ActiveCollector = (UriCollector) cbUrlCollectors.SelectedItem;
                btSettings.Text = Language.Instance.Settings;
            }
        }

        /// <summary>
        /// Method bound to cbUrlCollectors.SelectedIndexChanged event; Shows the correct information about the displayed urlCollector
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbUrlCollectors_SelectedIndexChanged(object sender, EventArgs e)
        {
            btSettings.Text = cbUrlCollectors.SelectedItem != UriCollector.ActiveCollector ? "Use" : "Settings";
            var curplug = (UriCollector)cbUrlCollectors.SelectedItem;
            var current = curplug.GetScriptInfoByCulture(CultureInfo.CurrentCulture); //todo localize cultureinfo
            pnlInfocontainer.Controls.Clear();
            pnlInfocontainer.Controls.Add(new PluginInformationPanel(current));
        }

        /// <summary>
        /// Method bound to Options.FormClosing event; Checks if any settings have been modified and commits them if the users excepts the changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Options_FormClosing(object sender, FormClosingEventArgs e)
        {
            //var extensions = Plugin<IExtension>.FindPlugins();
            var collectors = this.collectors;

            bool changed = PMC.Properties.Settings.Default.SettingsChanged || extensions.Any(c => c.Instance.Changed) ||
                           collectors.Any(c => c.Changed) || Website.Websites.Any(c => c.Changed) || UrlExpressions.Instance.Changed;

            if (!changed)
                return;

            var mes = MessageBox.Show(Language.Instance.SettingsChangedSave, Language.Instance.SaveSettings + "?", MessageBoxButtons.YesNoCancel);

            switch (mes)
            {
                case DialogResult.Yes:
                    UrlExpressions.Instance.Save();
                    Website.SaveAll();

                    foreach (var extension in extensions)
                    {
                        extension.Instance.Save();
                    }
                    collectors.Where(c => c.Changed).ForEach(c => c.Save());
                    //foreach (var collector in collectors)
                    //{
                    //    collector.Save();
                    //}
                    PMC.Properties.Settings.Default.Save();

                    break;
                case DialogResult.No:
                    UrlExpressions.Instance.Reload();
                    Website.DiscardChanges();
                    PMC.Properties.Settings.Default.Reload();

                    foreach (var extension in extensions)
                    {
                        extension.Instance.Reload();
                    }

                    foreach (var collector in collectors)
                    {
                        collector.Reload();
                    }

                    break;
                case DialogResult.Cancel:
                    e.Cancel = true;
                    break;
            }
        }

        //Todo delete
        /// <summary>
        /// Method bound to cbProtectedMode.CheckChanged event; Enables/Disables protected mode in IE
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbProtectedMode_CheckedChanged(object sender, EventArgs e)
        {
            Global.SetIEProtectedMode(!cbProtectedMode.Checked);
        }
    }
}
